/*
 * helperfuncs.c
 * CS 3251 Programming Assignment #2
 * Dustin Shiflett
 */ 

#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for gethostbyname(), socket(), connect(), sendto(), and recvfrom() */

#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() and alarm() */

#include <signal.h>     /* for sigaction() */
#include <sys/types.h>
#include <netinet/in.h>

#include "dropper.h"
#include "helperfuncs.h"

int debug = 0;

extern int sock;
extern FILE file;

void sigint() {
    
}

int calcRTO(int origRTO, int RTT) {
    /*Calculate Smooth Round Trip Time (SRTT) using Exponentially Weighted Moving Average (EWMA)
        SRTT_i+1 = (1-a)*SRTT[i-1]+a*RTT ; a = 1/8
        RFC793 TCP sets timeout to be 2*SRTT
        */
    int newSRTT, origSRTT;
    
    origSRTT = origRTO / 2;
    
    newSRTT = ((1 - SRTTCONSTANT) * origSRTT) + (SRTTCONSTANT * RTT);
    
    return (newSRTT * 2);
}

int fileExists(char* fileName) {
    int exists;
    FILE* file;
    
    exists = 0;
    if(file = fopen(fileName, "r")) {
        fclose(file);
        exists = 1;
    }
    
    return exists;
}
